package work20241226;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author zdl
 * @date 2024/12/26
 */
class ThreadCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i < 101; i++) {
            sum += i;
        }
        System.out.println(sum);
        return sum;
    }
}

public class Demo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //3. 使用Callable接口，计算1到100的累加和，并在主线程中输出结果。
        FutureTask<Integer> futureTask = new FutureTask<>(new ThreadCallable());
        new Thread(futureTask).start();
        //获取Callable接口的返回值
        System.out.println(futureTask.get());
    }
}
